package com.shujia.base.javamr;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Reduce {
    public static void main(String[] args) throws Exception {
        File file = new File("hadoop/data/maps");
        String[] maps = file.list();
        HashMap<String, Long> map1 = new HashMap<>();

        BufferedWriter bw = new BufferedWriter(new FileWriter("hadoop/data/reduce/part-r-00000"));
        BufferedReader br = null;
        if (maps != null) {
            for (String map : maps) {
                br = new BufferedReader(new FileReader("hadoop/data/maps/" + map));

                String line = null;
                while ((line = br.readLine()) != null) {
                    String clazz = line.split("\t")[0];
                    String number = line.split("\t")[1];
                    if(map1.containsKey(clazz)){
                        map1.put(clazz, map1.get(clazz) + Long.parseLong(number));
                    }else {
                        map1.put(clazz, Long.parseLong(number));
                    }
                }
            }
        }

        Set<Map.Entry<String, Long>> entries = map1.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            bw.write(entry.getKey() + "\t" + entry.getValue());
            bw.newLine();
            bw.flush();
        }


    }
}
